/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/spf13/cobra"
)

func active(url string, cmd *cobra.Command, args []string) {
	client := http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("创建请求失败: %v\n", err)
		return
	}

	q := req.URL.Query()
	name, _ := cmd.Flags().GetString("name")
	commit, _ := cmd.Flags().GetString("commit")
	force, _ := cmd.Flags().GetBool("force")
	q.Add("name", name)
	q.Add("commit", commit)
	q.Add("force", strconv.FormatBool(force))
	req.URL.RawQuery = q.Encode()

	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("请求失败: %v\n", err)
		return
	}
	defer resp.Body.Close()
	buf, _ := ioutil.ReadAll(resp.Body)
	fmt.Print(string(buf))
}

// activeCmd represents the active command
var public_activeCmd = &cobra.Command{
	Use: "active",
	Run: func(cmd *cobra.Command, args []string) {
		active(cmd_url_prefix+"/api/public/active", cmd, args)
	},
}

var service_activeCmd = &cobra.Command{
	Use: "active",
	Run: func(cmd *cobra.Command, args []string) {
		active(cmd_url_prefix+"/api/service/active", cmd, args)
	},
}

func init() {
	serviceCmd.AddCommand(service_activeCmd)
	publicCmd.AddCommand(public_activeCmd)
	service_activeCmd.Flags().String("name", "", "service name")
	public_activeCmd.Flags().String("name", "", "public name")

	service_activeCmd.Flags().Bool("force", false, "force active")
	public_activeCmd.Flags().Bool("force", false, "force active")

	// service_activeCmd.MarkFlagRequired("name")
	// public_activeCmd.MarkFlagRequired("name")
}
